package com.grj.parkingsystem.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.grj.parkingsystem.common.Result;
import com.grj.parkingsystem.entity.ParkingReservation;
import com.grj.parkingsystem.service.ParkingReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/reservation")
@Api(tags = "5. 车位预订管理")
public class ReservationController {

    @Autowired
    private ParkingReservationService reservationService;

    @PostMapping("/create")
    @ApiOperation(value = "创建预订", notes = "用户预订指定停车场的车位")
    @ApiOperationSupport(order = 1)
    public Result<?> createReservation(@ApiParam(value = "认证Token", required = true)
                                     @RequestHeader("Authorization") String token,
                                     @RequestParam Long parkingLotId,
                                     @RequestParam Long spaceId,
                                     @RequestParam String startTime,
                                     @RequestParam String endTime) {
        boolean reservation = reservationService.createReservation(token, parkingLotId, spaceId, startTime, endTime);
        if (!reservation) {
            return Result.error("预约失败");
        }
        return Result.success("预约成功");
    }

    @PostMapping("/cancel/{reservationId}")
    @ApiOperation(value = "取消预订", notes = "用户取消已创建的车位预订")
    @ApiOperationSupport(order = 2)
    public Result<?> cancelReservation(@ApiParam(value = "认证Token", required = true)
                                     @RequestHeader("Authorization") String token,
                                     @PathVariable Long reservationId) {
        reservationService.cancelReservation(token, reservationId);
        return Result.success("取消成功");
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取预订列表", notes = "获取当前用户的车位预订列表")
    @ApiOperationSupport(order = 3)
    public Result<?> listReservations(@ApiParam(value = "认证Token", required = true)
                                    @RequestHeader("Authorization") String token) {
        List<ParkingReservation> reservations = reservationService.getUserReservations(token);
        return Result.success(reservations);
    }
}